using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.IO;
using LeisureCenter.Api;
using LeisureCenter.Tools;
using LeisureCenter.MetadataHandlers;

namespace LeisureCenter.BrowserItems
{
    public class LibraryVideoBrowserObjectItem : LibraryBrowserObjectItem
    {
        public LibraryVideoBrowserObjectItem(IBrowserFolderItem folder, string fileName, string text)
            : base(folder, fileName, text)
        {
        }

        public override string IconName
        {
            get { return @"video"; }
        }
    }

    public abstract class BaseLibraryVideoBrowserFolderItem : BaseLibraryBrowserFolderItem
    {
        protected BaseLibraryVideoBrowserFolderItem(IBrowserFolderItem folder, string text, QueryItems queryItems)
            : base(folder, text, queryItems)
        {
            this.tables.Add("VideoFilesMetaData");
            this.wheres.Add(@"VideoFilesMetaData.Id = Files.Id");
            this.wheres.Add(@"(Files.Deleted ISNULL OR Files.Deleted = 0)");
        }

        protected override void CreateGeneralItems()
        {
            this.items.Add(new LibraryVideoUnseenSubtitledDynamicPlaylistFolderItem(this, @"Unseen and subtitled", new QueryItems(this.tables, this.wheres, null, this.titleParts)));
            this.items.Add(new LibraryVideoUnseenDynamicPlaylistFolderItem(this, @"Unseen", new QueryItems(this.tables, this.wheres, null, this.titleParts)));
        }
    }

    public class LibraryVideoTitleBrowserFolderItem : BaseLibraryVideoBrowserFolderItem
    {
        public LibraryVideoTitleBrowserFolderItem(IBrowserFolderItem folder, string text, QueryItems queryItems, string title)
            : base(folder, text, queryItems)
        {
            this.titleParts.Add(title);
        }

        protected override void CreateItems()
        {
            ExecuteQuery(String.Format(@"SELECT DISTINCT VideoFilesMetaData.Title, VideoFilesMetaData.Number, VideoFilesMetaData.SubNumber, VideoFilesMetaData.Subtitle, Files.Filename FROM {0} {1} ORDER BY VideoFilesMetaData.Number, VideoFilesMetaData.SubNumber COLLATE NOCASE", GetTables(), GetWhereCondition()));
        }

        protected override void ReadQueryItem(SQLiteDataReader reader)
        {
            string text = reader.GetValue(0).ToString();
            if (!reader.IsDBNull(1))
                text += string.Format(@" - {0}x{1:00}", reader.GetValue(1), reader.GetValue(2));
            string subtitle = reader.GetValue(3).ToString();
            if (!string.IsNullOrEmpty(subtitle))
                text += " - " + subtitle;
            this.items.Add(new LibraryVideoBrowserObjectItem(this, reader.GetValue(4).ToString(), text));
        }
    }

    public class LibraryVideoTitlesBrowserFolderItem : BaseLibraryVideoBrowserFolderItem
    {
        public LibraryVideoTitlesBrowserFolderItem(IBrowserFolderItem folder, string text, QueryItems queryItems)
            : base(folder, text, queryItems)
        {
        }

        protected override void CreateItems()
        {
            ExecuteQuery(String.Format(@"SELECT DISTINCT VideoFilesMetaData.Title FROM {0} {1} ORDER BY VideoFilesMetaData.Title COLLATE NOCASE", GetTables(), GetWhereCondition()));
        }

        protected override void ReadQueryItem(SQLiteDataReader reader)
        {
            List<string> wheres = new List<string>(this.wheres);
            string title = reader.GetValue(0).ToString();
            wheres.Add(String.Format(@"VideoFilesMetaData.Title = '{0}'", SQLiteTools.EscapeSqlString(title)));
            this.items.Add(new LibraryVideoTitleBrowserFolderItem(this, title, new QueryItems(this.tables, wheres, null, this.titleParts), title));
        }
    }

    public abstract class BaseLibraryVideoDynamicPlaylistFolderItem : BaseLibraryBrowserFolderItem
    {
        protected BaseLibraryVideoDynamicPlaylistFolderItem(IBrowserFolderItem folder, string text, QueryItems queryItems)
            : base(folder, text, queryItems)
        {
            this.tables.Add("VideoFilesMetaData");
            this.wheres.Add(@"VideoFilesMetaData.Id = Files.Id");
            this.wheres.Add(@"(Files.Deleted ISNULL OR Files.Deleted = 0)");
        }

        protected override void ReadQueryItem(SQLiteDataReader reader)
        {
            this.items.Add(new LibraryAudioBrowserObjectItem(this, reader.GetValue(0).ToString(), reader.GetValue(1).ToString()));
        }

        public override string IconName { get { return @"library"; } }
    }

    public class LibraryVideoUnseenDynamicPlaylistFolderItem : BaseLibraryVideoDynamicPlaylistFolderItem
    {
        public LibraryVideoUnseenDynamicPlaylistFolderItem(IBrowserFolderItem folder, string text, QueryItems queryItems)
            : base(folder, text, queryItems)
        {
            this.tables.Add("Playings");
            this.wheres.Add("Playings.id ISNULL");
            this.titleParts.Add(text);
        }

        protected override void CreateItems()
        {
            ExecuteQuery(String.Format(@"SELECT VideoFilesMetaData.Title, VideoFilesMetaData.Number, VideoFilesMetaData.SubNumber, VideoFilesMetaData.Subtitle, Files.Filename FROM Files, VideoFilesMetaData LEFT OUTER JOIN Playings ON Playings.Id = Files.Id {0} ORDER BY Files.AddedDatetime ASC", GetWhereCondition()));
        }

        protected override void ReadQueryItem(SQLiteDataReader reader)
        {
            string text = reader.GetValue(0).ToString();
            if (!reader.IsDBNull(1))
                text += string.Format(@" - {0}x{1:00}", reader.GetValue(1), reader.GetValue(2));
            string subtitle = reader.GetValue(3).ToString();
            if (!string.IsNullOrEmpty(subtitle))
                text += " - " + subtitle;
            AddItem(reader.GetValue(4).ToString(), text);
        }

        protected virtual void AddItem(string fileName, string text)
        {
            this.items.Add(new LibraryVideoBrowserObjectItem(this, fileName, text));
        }
    }

    public class LibraryVideoUnseenSubtitledDynamicPlaylistFolderItem : LibraryVideoUnseenDynamicPlaylistFolderItem
    {
        public LibraryVideoUnseenSubtitledDynamicPlaylistFolderItem(IBrowserFolderItem folder, string text, QueryItems queryItems)
            : base(folder, text, queryItems)
        {
        }

        protected override void AddItem(string fileName, string text)
        {
            if (File.Exists(Path.ChangeExtension(fileName, ".srt")) || File.Exists(Path.ChangeExtension(fileName, ".ssa")) || File.Exists(Path.ChangeExtension(fileName, ".idx")))
                base.AddItem(fileName, text);
        }
    }

    public class LibraryVideoBrowserFolderItem : BaseLibraryVideoBrowserFolderItem
    {
        public LibraryVideoBrowserFolderItem(long libraryId)
            : base(null, "", new QueryItems(null, null, null, null))
        {
            if (libraryId != 0)
                this.wheres.Add("Files.LibraryId = " + libraryId);
        }

        protected override void CreateItems()
        {
            this.items = new List<IBrowserItem>();
            List<string> wheres = new List<string>(this.wheres);
            wheres.Add(string.Format("VideoFilesMetaData.Type = {0}", (int)VideoType.TvShow));
            this.items.Add(new LibraryVideoTitlesBrowserFolderItem(this, @"TV Shows", new QueryItems(this.tables, wheres, null, null)));
            wheres = new List<string>(this.wheres);
            wheres.Add(string.Format("VideoFilesMetaData.Type = {0}", (int)VideoType.Movie));
            this.items.Add(new LibraryVideoTitlesBrowserFolderItem(this, @"Movies", new QueryItems(this.tables, wheres, null, null)));
            wheres = new List<string>(this.wheres);
            wheres.Add(string.Format("VideoFilesMetaData.Type = {0}", (int)VideoType.VideoClip));
            this.items.Add(new LibraryVideoTitlesBrowserFolderItem(this, @"Video Clips", new QueryItems(this.tables, wheres, null, null)));
            wheres = new List<string>(this.wheres);
            wheres.Add(string.Format("(VideoFilesMetaData.Type ISNULL OR VideoFilesMetaData.Type = {0})", (int)VideoType.Other));
            this.items.Add(new LibraryVideoTitlesBrowserFolderItem(this, @"Other", new QueryItems(this.tables, wheres, null, null)));
        }

        protected override void ReadQueryItem(SQLiteDataReader reader)
        {
        }
    }
}
